﻿using System;
using System.Collections;
using System.Text;

namespace AiMoInGH.PrinterForm
{
    class PrinterManager
    {
        readonly StringBuilder m_StringBuilder = new StringBuilder();
        bool m_PreviewWindowOn = false;
        PrinterForm form = null;

        int m_PrintCount = 0;
        public void ShowWindow(object obj, EventArgs args)
        {
            if (form == null || form.Disposing || form.IsDisposed)
            {
                form = new PrinterForm(m_StringBuilder);
                form.TopMost = true;
                form.Show();
                form.FormClosing += FormClosing;
                m_PreviewWindowOn = true;
                form.SyncContent();
            }
        }

        void FormClosing(object sender, EventArgs e)
        {
            m_PreviewWindowOn = false;
            form = null;
        }
        public void Clear()
        {
            m_PrintCount = 0;
            m_StringBuilder.Clear();
            form?.SyncContent();
        }
        DateTime previousSyncTime = DateTime.MinValue;
        void TrySync()
        {
            m_PrintCount++;
            if (m_PreviewWindowOn)
            {
                DateTime now = DateTime.Now;
                if ((now - previousSyncTime).TotalMilliseconds > 200)
                {
                    previousSyncTime = now;
                    DoSync();
                }
            }
        }
        public void DoSync()
        {
            if (m_PreviewWindowOn) form?.SyncContent();
        }

        public void Add(string text)
        {
            m_StringBuilder.AppendLine(text);
            TrySync();
        }
        public void Add(string content, params object[] args)
        {
            m_StringBuilder.AppendLine(string.Format(content, args));
            TrySync();
        }
        public void Add(object obj)
        {
            if (obj == null) m_StringBuilder.AppendLine("null");
            else if (obj is IEnumerable iEnumerable)
            {
                string name = iEnumerable.GetType().Name;
                name = name.Substring(0, name.Length - 2);

                string content = $"{name}" + " { ";
                int count = 0;
                foreach (object obj2 in iEnumerable)
                {
                    if (count == 0)
                    {
                        content += obj2.ToString();
                    }
                    else if (count < 10)
                    {
                        content += ", " + obj2.ToString();
                    }
                    else if (count == 10)
                    {
                        content += $", ......";
                    }
                    count++;
                }
                content += " } " + $" ({count})";
                m_StringBuilder.AppendLine(content);
            }
            else if (obj is string ss)
            {
                m_StringBuilder.AppendLine(ss);
            }
            else
            {
                m_StringBuilder.AppendLine($"[{obj.GetType()}] {obj}");
            }
            TrySync();
        }
    }
}